package com.mspbots.rule.queue.ticket;

import com.mspbots.api.client.WiseClient;
import com.mspbots.cw.request.TenantTicketDTO;
import com.mspbots.cw.request.TenantUserDTO;
import com.mspbots.rule.exceptions.BaseRule;
import com.mspbots.rule.exceptions.ticket.TicketBaseRule;
import com.mspbots.rule.queue.BaseQueue;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.api.Facts;
import org.jeasy.rules.api.Rules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

import static java.time.temporal.ChronoUnit.SECONDS;

/**
 * @ClassName com.mspbots.rule.queue.timeentry.BaseTrigger
 * @Description TODO
 * @Author Jun
 * @Date 2019/8/30 18:38
 * @Version 1.0
 **/
@Slf4j
@Component
public class TicketBaseQueue extends BaseQueue {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private WiseClient wiseClient;

    protected void onRule(String ruleName, TenantTicketDTO tenantTicketDTO) {
        try {
            if (checkRule(ruleName, tenantTicketDTO.getTenantId())) {
                if (tenantTicketDTO.getTenantId() != null && tenantTicketDTO.getOwner() != null) {
                    Map<String, Object> params = new HashMap<>();
                    params.put("tenantId", tenantTicketDTO.getTenantId());
                    params.put("cwUid", tenantTicketDTO.getOwner());
                    TenantUserDTO tenantUserDTO = coreClient.get("/" + tenantTicketDTO.getTenantId() + "/user/info", params, TenantUserDTO.class).block(Duration.of(5, SECONDS));
                    if (tenantUserDTO != null) {
                        tenantTicketDTO.setTenantUserId(tenantUserDTO.getId());
                        Rules rules = new Rules();
                        Class clazz = Class.forName(tenantRuleDTO.getClassName());
                        TicketBaseRule instance = (TicketBaseRule) clazz.getDeclaredConstructor().newInstance();
                        instance.init(tenantRuleDTO, messageProducer, messagesService, tenantUserMappingService,
                                tenantConfigurationService, coreClient, objectMapper);
                        instance.setStringRedisTemplate(stringRedisTemplate);
                        instance.setWiseClient(wiseClient);
                        rules.register(instance);
                        Facts facts = new Facts();
                        facts.put(BaseRule.FACT, tenantTicketDTO);
                        rulesEngine.fire(rules, facts);
                    } else {
                        log.info("ticket tenantUserDTO is null {}", tenantTicketDTO.getCwId());
                    }
                } else {
                    log.info("ticket owner is null {}", tenantTicketDTO.getCwId());
                }
            }
        } catch (Exception e) {
            log.error("trigger [{}] fire error : {}", ruleName, e.getMessage());
            e.printStackTrace();
        }
    }
}
